package org.example.flinktest.operators;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * @author SHB
 */
public class LineSplitter implements FlatMapFunction<String, Tuple2<String, Integer>> {
  private String separator;
  public LineSplitter() {
    this("\\s+");
  }

  public LineSplitter(String separator) {
    this.separator=separator;
  }

  @Override
  public void flatMap(String line, Collector<Tuple2<String, Integer>> collector) {
    for(String word:line.split(this.separator)){
      collector.collect(new Tuple2<>(word, 1));
    }
  }
}
